package com.jic.market.common.util;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JacksonUtils {
    private static final Logger log = LoggerFactory.getLogger(JacksonUtils.class);
    private static ObjectMapper objectMapper = new ObjectMapper();

    public JacksonUtils() {
    }

    public static <T> T fromJson(String json, Class<T> clazz) {
        try {
            return json == null ? null : fromJsonWithException(json, clazz);
        } catch (Exception var3) {
            log.warn("deserialize from json fail: {}", json, var3);
            return null;
        }
    }

    public static <T> T fromJson(String json, Class<T> c, Class<?>... t) throws JacksonUtils.JacksonException {
        try {
            return json == null ? null : fromJsonWithException(json, c, t);
        } catch (IOException var4) {
            throw new JacksonUtils.JacksonException(var4);
        }
    }

    public static <T> T fromJsonWithException(String json, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException {
        return objectMapper.readValue(json, clazz);
    }

    public static <T> T fromJsonWithException(String json, Class<T> c, Class<?>... t) throws JsonParseException, JsonMappingException, IOException {
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(c, t);
        return objectMapper.readValue(json, javaType);
    }

    public static <T> List<T> fromJsonList(String json, Class<T> t) {
        try {
            return StringUtils.isBlank(json) ? null : fromJsonListWithException(json, t);
        } catch (Exception var3) {
            log.warn("deserialize to list from json fail, json={}", json, var3);
            return null;
        }
    }

    public static <T> List<T> fromJsonListWithException(String json, Class<T> c) throws JacksonUtils.JacksonException {
        try {
            JavaType type = getCollectionType(ArrayList.class, c);
            return (List)objectMapper.readValue(json, type);
        } catch (Exception var3) {
            throw new JacksonUtils.JacksonException(var3);
        }
    }

    public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
        return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }

    public static String toJsonWithException(Object o) throws JsonProcessingException {
        return objectMapper.writeValueAsString(o);
    }

    public static String toJson(Object o) {
        if (o == null) {
            return null;
        } else {
            try {
                return toJsonWithException(o);
            } catch (Exception var2) {
                throw new JacksonUtils.JacksonException(var2);
            }
        }
    }

    public static Map<String, Object> jsonToMap(String json) {
        if (StringUtils.isBlank(json)) {
            return null;
        } else {
            Object map = new HashMap();

            try {
                map = (Map)objectMapper.readValue(json, HashMap.class);
            } catch (Exception var3) {
                log.warn("json to map failed", var3);
            }

            return (Map)map;
        }
    }

    public static <T, K> Map<T, K> convertValue(Object req, Class<T> keyClazz, Class<K> valueClazz) {
        Map<T, K> ret = (Map)objectMapper.convertValue(req, objectMapper.getTypeFactory().constructMapType(Map.class, keyClazz, valueClazz));
        return ret;
    }

    public static <T> T convertMap(Map map, Class<T> retClazz) {
        return map == null ? null : objectMapper.convertValue(map, retClazz);
    }

    static {
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public static class JacksonException extends RuntimeException {
        private static final long serialVersionUID = 2419465011453289773L;

        public JacksonException(String msg) {
            super(msg);
        }

        public JacksonException(Throwable cause) {
            super(cause);
        }

        public JacksonException(String msg, Throwable cause) {
            super(msg, cause);
        }
    }
}
